package com.sgb.kjwl.view.ui.activity.packers;

import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;

import android.os.Build;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;
import android.widget.Toast;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPackersPersonDetailBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.PackersPersonDetailEntity;
import com.sgb.kjwl.model.entity.PackersSkillsEntity;
import com.sgb.kjwl.view.ui.fragment.packers.PersonIntroductionFragment;
import com.sgb.kjwl.viewmodel.packers.PackersPersonDetailModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.DialogUtil;
import com.swgk.core.util.MDateUtil;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

/**
 * @author pangyuheng
 * @date 2019/7/9
 * description:人才详情页
 */
@Deprecated
public class PackersPersonDetailActivity extends AppActivity {
    public static final String TAG = "PackersPersonDetailActivity";
    private ActivityPackersPersonDetailBinding binding;
    private FragmentManager fragmentManager;
    private final Fragment[] myFragment = new Fragment[2];

    public static final int FRAGMENT_PERSON_INTRODUCTION = 0;
    private int currentIndex = 0;

    private String mobile = "";
    private String name = "";

    @Inject
    PackersPersonDetailModel viewModel;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this)).build().inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = DataBindingUtil.setContentView(PackersPersonDetailActivity.this, R.layout.activity_packers_person_detail);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        String id = getIntent().getStringExtra("id");
        initFragment();
        viewModel.getCurrentUserInfo(id);

        binding.tvKefuMobile.setOnClickListener(this);
        binding.tvPersonMobile.setOnClickListener(this);
        binding.ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_kefu_mobile) {
            DialogUtil.showTelDialog(this, "客服", "4000010051");
        } else if (id == R.id.tv_person_mobile) {
            if ("".equals(mobile)) {
                Toast.makeText(PackersPersonDetailActivity.this, "此联系人无号码，请咨询客服核实", Toast.LENGTH_SHORT).show();
            } else {
                DialogUtil.showTelDialog(this, name, mobile);
            }
        }
    }

    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        myFragment[FRAGMENT_PERSON_INTRODUCTION] = new PersonIntroductionFragment();
        fragmentManager.beginTransaction().add(R.id.fl_container, myFragment[FRAGMENT_PERSON_INTRODUCTION]).commit();
    }

    /**
     * 切换fragment
     * @param index fragment集合的下标
     */
    public void displayFrg(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (!myFragment[index].isAdded()) {
            transaction.hide(myFragment[currentIndex]).add(R.id.fl_container, myFragment[index]).commitAllowingStateLoss();
        } else {
            transaction.hide(myFragment[currentIndex]).show(myFragment[index]).commitAllowingStateLoss();
        }
        currentIndex = index;
    }

    public void showPersonDetail(PackersPersonDetailEntity data) {
        if (data.getConcatPhone() != null && !"".equals(data.getConcatPhone())){
            mobile = data.getConcatPhone();
        }
        if (data.getConcat() != null && !"".equals(data.getConcat())){
            name = data.getConcat();
        }
        binding.tvProfession.setText(data.getTitle());
        binding.tvTime.setText(MDateUtil.getDate(String.valueOf(data.getSubmitTime())));

        //传递数据到fragment
        EventBus.getDefault().postSticky(data);
    }

    public void showPersonSkillsDetail(PackersSkillsEntity data) {
        //传递数据到fragment
        EventBus.getDefault().postSticky(data);
    }

}
